package com.cnpinxi.service;

import com.cnpinxi.dao.AdminUserMapper;
import com.cnpinxi.domain.AdminUser;
import com.cnpinxi.log.LogFactory;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.service.*;
import com.cnpinxi.util.MD5Tools;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/1/2.
 */
@Service
public class AdminUserServiceImpl implements com.cnpinxi.service.AdminUserService {
    protected Logger logger = LogFactory.getLogger(this.getClass());
    @Autowired
    AdminUserMapper adminUserMapper;



    @Override
    public AdminUser login(Map<String, String> map) {
        String passWord = map.get("passWord");
        String userName = map.get("userName");
        map.put("passWord", MD5Tools.MD5(passWord));
        map.put("userName", userName);
        map.put("status", String.valueOf(AdminUser.STATUS_VALID));
        return adminUserMapper.selectAdminUser(map);
    }

    @Override
    public List<Map<String, Object>> getUserList(Map<String, String> map) {
        return adminUserMapper.selectAdminUserList(map);
    }

    @Override
    public AdminUser getUser(Map<String, String> map) {
        if(!map.containsKey("tid")){
           throw new MyRunTimeExcption("tid is null");
        }
            return adminUserMapper.selectAdminUser(map);

    }

    @Override
    public AdminUser selectAdminById(Map<String,String> map) {
        return adminUserMapper.selectAdminById(map);
    }

    @Override
    public void modifyUser(Map<String, String> map) {
        AdminUser adminUser = null;
        String tid = map.get("tid");
        if (StringUtils.isBlank(tid)) {
            adminUser = new AdminUser();
            adminUser.setCreateTime(new Date());
            adminUser.setPassWord(MD5Tools.MD5("123456"));
        } else {
            Map<String,String> userMap=new HashMap<>();
            logger.info(tid);
            userMap.put("tid",tid);
            adminUser = this.getUser(userMap);
        }
        String userName = map.get("userName");
        if (StringUtils.isNotBlank(userName)) {
            adminUser.setUserName(userName);
        }
        String passWord = map.get("passWord");
        if (StringUtils.isNotBlank(passWord)) {
            adminUser.setPassWord(MD5Tools.MD5(passWord));
        }
        String realName = map.get("realName");
        if (StringUtils.isNotBlank(realName)) {
            adminUser.setRealName(realName);
        }
        String roleId = map.get("roleId");
        if (StringUtils.isNotBlank(roleId)) {
            adminUser.setRoleId(Integer.parseInt(roleId));
        }  String status = map.get("status");
        if (StringUtils.isNotBlank(status)) {
            adminUser.setStatus(Byte.parseByte(status));
        }

        if (StringUtils.isBlank(tid)) {
            adminUserMapper.insertAdminUser(adminUser);
        } else {
            adminUserMapper.updateAdminUserById(adminUser);
        }
    }
}
